#include<stdio.h>
#include<stdlib.h>
#include<string.h>


//按行 操作文件
void test(){
    FILE * f_write = fopen("./test2.txt","w");
    if(f_write == NULL){
        return;
    }
    char * buf[] = {
        "afdafdfa\n",
        "adsffweafa\n",
        "dswevssaa\n"
    };

    for(int i=0;i<3;i++){
        fputs(buf[i], f_write);
    }

    fclose(f_write);

    FILE * f_read = fopen("./test2.txt","r");
    if(f_read ==NULL){
        return;
    }
 
    while(!feof(f_read)){
        char buf[1024] = {0};
        fgets(buf,1024,f_read);
        printf("%s", buf);
    }
    fclose(f_read);
}

struct Hero{
    char name[64];
    int age;
};

// 按块
void test2(){
    // 二进制写
    FILE * fb_write = fopen("./test3.txt","wb");
    if(fb_write==NULL){
        return;
    }

    struct Hero h[4] = {
        {"我",21},
        {"饿",20},
        {"了",23}
    };

    for(int i=0;i<4;i++){
        // 数据地址   块大小   块数量   文件指针
        fwrite(&h[i], sizeof(struct Hero), 1, fb_write);
    }

    fclose(fb_write);

    FILE * fb_read = fopen("./test3.txt","rb");
    if(fb_read ==NULL){
        return;
    }

    struct Hero temp[4];
    fread(&temp, sizeof(struct Hero), 4, fb_read);
    for(int i=0;i<4;i++){
        printf("姓名：%s 年龄： %d\n", temp[i].name, temp[i].age);
    }

    fclose(fb_read);

}

// 按照格式化读写文件 fprintf() fscanf()

// ftell(), fseek(), rewind();

//SEEK_SET

void test4(){

#if 0


#endif

}

void main(){

    // test();

    test2();

}